package bank;

import java.util.Date;

import database.CustomerDBAdapter;

/**
 * 
 */

/**
 * @author Dennis Post
 * 
 */

public class Customer {

	private String name;
	private String prename;
	private Date birthdate;
	private String street;
	private String number;
	private String postal;
	private String town;
	private String country;
	private Date creationDate;
	private Employer employer;
	private int customerID;

	/**
	 * Customer Constructor without given adress
	 * 
	 * @param name
	 * @param prename
	 * @param creationDate
	 * @param employer
	 */
	public Customer(String name, String prename, Date birthdate, Date creationDate, Employer employer) {
		this(name, prename, birthdate, null, null, null, null, null, creationDate, employer);
	}

	/**
	 * Customer Constructor with given adress
	 * 
	 * @param name
	 * @param prename
	 * @param street
	 * @param number
	 * @param postal
	 * @param town
	 * @param country
	 * @param creationDate
	 * @param employer
	 */
	public Customer(String name, String prename, Date birthdate, String street, String number, String postal,
			String town, String country, Date creationDate, Employer employer) {
		int insertedID = CustomerDBAdapter.createCustomer(prename, name, birthdate, street, number, postal, town,
				country, creationDate, employer);

		if (insertedID > 0) {

			this.name = name;
			this.prename = prename;
			this.birthdate = birthdate;
			this.street = street;
			this.number = number;
			this.postal = postal;
			this.town = town;
			this.country = country;
			this.creationDate = creationDate;
			this.employer = employer;
			this.customerID = insertedID;
		}

		else {
			System.out.println("Fehler Customer!");
		}
	}

	/**
	 * Get name and prename of the customer as a formatted String
	 * 
	 * @return "name, prename"
	 */
	public String getFormattedName() {
		return this.name + ", " + this.prename;
	}

	// CHANGES

	/**
	 * Changes the adress of the customer to the given value
	 * 
	 * @param street
	 * @param number
	 * @param postal
	 * @param town
	 * @param country
	 * @return Change succeeded? true/false
	 */
	public final boolean changeCustomerAdress(String street, String number, String postal, String town, String country) {
		if (CustomerDBAdapter.changeCustomer(this.name, this.prename, this.birthdate, street, number, postal, town,
				country, this.creationDate, this.employer) == 1) {
			this.street = street;
			this.number = number;
			this.postal = postal;
			this.town = town;
			this.country = country;

			return true;
		}

		return false;
	}

	/**
	 * Changes the name of the customer to the given value
	 * 
	 * @param prename
	 * @param name
	 * @return Change succeeded? true/false
	 */
	public final boolean changeCustomerName(String prename, String name) {
		if (CustomerDBAdapter.changeCustomer(prename, name, this.birthdate, this.street, this.number, this.postal,
				this.town, this.country, this.creationDate, this.employer) == 1) {
			this.prename = prename;
			this.name = name;

			return true;
		}

		return false;
	}

}
